﻿@using Masa.Auth.Contracts.Admin.Infrastructure.Constants
@inherits ClientSectionCompontentBase

<div class="mr-2">
    <MRow Class="pt-4">
        <MCol Md=6>
            <SNumberTextField @bind-Value="Dto.IdentityTokenLifetime" Label="@T("IdentityTokenLifetime")" Tooltip="@T("IdentityTokenLifetimeTips")" />
        </MCol>
        <MCol Md=6>
            <SSelect @bind-Value="@Dto.AllowTokenSigningAlgorithms"
                     Label="@T("AllowedIdentityTokenSigningAlgorithms")" Tooltip="@T("AllowedIdentityTokenSigningAlgorithmsTips")"
                     Multiple
                     Items="@Dto.AllowedIdentityTokenSigningAlgorithmsItems"
                     ItemText="u => u.Text"
                     ItemValue="u => u.Value">
            </SSelect>
        </MCol>
        <MCol Md=6>
            <SNumberTextField @bind-Value="Dto.AccessTokenLifetime" Label="@T("AccessTokenLifetime")" Tooltip="@T("AccessTokenLifetimeTips")" />
        </MCol>
        <MCol Md=6>
            <SSelect @bind-Value="@Dto.AccessTokenType"
                     Label="@T("AccessTokenType")" Tooltip="@T("AccessTokenTypeTips")"
                     Items="@_accessTokenType"
                     ItemText="u => u.Text"
                     ItemValue="u => u.Value">
            </SSelect>
        </MCol>
        <MCol Md=6>
            <SNumberTextField @bind-Value="Dto.AuthorizationCodeLifetime" Label="@T("AuthorizationCodeLifetime")" Tooltip="@T("AuthorizationCodeLifetimeTips")" />
        </MCol>
        <MCol Md=6>
            <SNumberTextField @bind-Value="Dto.AbsoluteRefreshTokenLifetime" Label="@T("AbsoluteRefreshTokenLifetime")" Tooltip="@T("AbsoluteRefreshTokenLifetimeTips")" />
        </MCol>
        <MCol Md=6>
            <SNumberTextField @bind-Value="Dto.SlidingRefreshTokenLifetime" Label="@T("SlidingRefreshTokenLifetime")" Tooltip="@T("SlidingRefreshTokenLifetimeTips")" />
        </MCol>
        <MCol Md=6>
            <SSelect @bind-Value="@Dto.RefreshTokenUsage"
                     Label="@T("RefreshTokenUsage")" Tooltip="@T("RefreshTokenUsageTips")"
                     Items="@_refreshTokenUsage"
                     ItemText="u => u.Text"
                     ItemValue="u => u.Value">
            </SSelect>
        </MCol>
        <MCol Md=6>
            <SSelect @bind-Value="@Dto.RefreshTokenExpiration"
                     Label="@T("RefreshTokenExpiration")" Tooltip="@T("RefreshTokenExpirationTips")"
                     Items="@_refreshTokenExpiration"
                     ItemText="u => u.Text"
                     ItemValue="u => u.Value">
            </SSelect>
        </MCol>
        <MCol Md=6>
            <STextField @bind-Value="Dto.PairWiseSubjectSalt" Label="@T("PairWiseSubjectSalt")" Tooltip="@T("PairWiseSubjectSaltTips")" />
        </MCol>
        <MCol Md=6>
            <SCheckbox Dense @bind-Value="Dto.AllowOfflineAccess" Label="@T("AllowOfflineAccess")" Tooltip="@T("AllowOfflineAccessTips")"></SCheckbox>
        </MCol>
        <MCol Md=6>
            <SCheckbox Dense @bind-Value="Dto.IncludeJwtId" Label="@T("IncludeJwtId")" Tooltip="@T("IncludeJwtIdTips")" />
        </MCol>
        <MCol Md=6>
            <SCheckbox Dense @bind-Value="Dto.UpdateAccessTokenClaimsOnRefresh" Label="@T("UpdateAccessTokenClaimsOnRefresh")" Tooltip="@T("UpdateAccessTokenClaimsOnRefreshTips")" />
        </MCol>
        <MCol Md=6>
            <SCheckbox Dense @bind-Value="Dto.AlwaysIncludeUserClaimsInIdToken" Label="@T("AlwaysIncludeUserClaimsInIdToken")" Tooltip="@T("AlwaysIncludeUserClaimsInIdTokenTips")" />
        </MCol>
        <MCol Md=6>
            <SCheckbox Dense @bind-Value="Dto.AllowAccessTokensViaBrowser" Label="@T("AllowAccessTokensViaBrowser")" Tooltip="@T("AllowAccessTokensViaBrowserTips")" />
        </MCol>
        <MCol Md=6>
            <SCheckbox Dense @bind-Value="Dto.AlwaysSendClientClaims" Label="@T("AlwaysSendClientClaims")" Tooltip="@T("AlwaysSendClientClaimsTips")" />
        </MCol>
    </MRow>
    <MRow>
        <MCol>
            <STextField @bind-Value="Dto.ClientClaimDto.Type" Label="@T("Type")" />
        </MCol>
        <MCol>
            <STextField @bind-Value="Dto.ClientClaimDto.Value" Label="@T("Value")" />
        </MCol>
        <MCol Md="@("auto")" Class="d-flex align-center">
            <SButton Class="mr-4" Icon OnClick="AddClaim" Color="regular3">
                <SIcon Size=32>@IconConstants.Add</SIcon>
            </SButton>
        </MCol>
        <MCol Md=12>
            <MDataTable Headers="_headers" Items="Dto.Claims" TItem="ClientClaimDto" HideDefaultFooter DisablePagination Dense Class="margin-border">
                <HeaderColContent Context="header">
                    <span class="body2">@header.Text</span>
                </HeaderColContent>
                <ItemColContent>
                    @switch (context.Header.Value)
                    {
                        case "Action":
                            <SButton Class="mr-4" Icon OnClick="()=>Dto.Claims.Remove(context.Item)" Color="regular">
                                <SIcon Tooltip="@T("Delete")">mdi-close</SIcon>
                            </SButton>
                            break;
                        default:
                            <span class="subtitle">@context.Value</span>
                            break;
                    }
                </ItemColContent>
            </MDataTable>
        </MCol>
    </MRow>
</div>

@code {
    [Parameter]
    public ClientTokenDto Dto { get; set; } = new();

    [Parameter]
    public EventCallback<ClientTokenDto> DtoChanged { get; set; }

    List<SelectItemDto<int>> _refreshTokenUsage = ClientConsts.GetTokenUsage();
    List<SelectItemDto<int>> _accessTokenType = ClientConsts.GetAccessTokenTypes();
    List<SelectItemDto<int>> _refreshTokenExpiration = ClientConsts.GetTokenExpirations();
    List<DataTableHeader<ClientClaimDto>> _headers { get; set; } = default!;

    protected override void OnInitialized()
    {
        _headers = new()
        {
            new() { Text = T("Type"), Value = nameof(ClientClaimDto.Type), Sortable = false },
            new() { Text = T("Value"), Value = nameof(ClientClaimDto.Value), Sortable = false },
            new() { Text = T("Action"), Value = "Action", Sortable = false,Align = DataTableHeaderAlign.Center },
        };
        Dto.AllowedIdentityTokenSigningAlgorithmsItems = ClientConsts.SigningAlgorithms();
        base.OnInitialized();
    }

    private void AddClaim()
    {
        if (string.IsNullOrWhiteSpace(Dto.ClientClaimDto.Type) || string.IsNullOrWhiteSpace(Dto.ClientClaimDto.Value))
        {
            return;
        }
        if (Dto.Claims.Any(p => p.Type == Dto.ClientClaimDto.Type.Trim()))
        {
            return;
        }
        Dto.Claims.Add(Dto.ClientClaimDto);
        Dto.ClientClaimDto = new();
    }
}
